﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MicroTest
{
    public partial class FrmWait : Form
    {
        private OperationMode OperationMode;
        private Form ParentFrm;
        public FrmWait(OperationMode mode,Form frm)
        {
            InitializeComponent();
            OperationMode = mode;
            ParentFrm = frm;
        }        
        private void FrmWait_Load(object sender, EventArgs e)
        {
            switch (OperationMode)
            {
                case OperationMode.UploadMt:
                    labTip.Text = "正在上传数据，请稍后";
                    break;
                case OperationMode.LoadSym:
                    labTip.Text = "正在加载课件，请稍后";

                    break;
                case OperationMode.ExportWriting:
                    {
                        labTip.Text = "正在保存，请稍后   ";
                        FrmClass frm = ParentFrm as FrmClass;
                        this.Top = frm.Height / 2 - (int)(2.5*this.Height);
                        this.Left = frm.Width / 2 - this.Width / 2;
                    }
                    break;
                default:
                    break;
            }
            labTip.Top = Height / 3;
            labTip.Left = (Width - labTip.Width-label2.Width) / 2;
            timer1.Start();
            label2.Left = labTip.Right;
            label2.Top = labTip.Top - (label2.Height - labTip.Height) / 2;
        }
        const int WM_NCLBUTTONDOWN = 0xA1;
        const int HT_CAPTION = 0x2;
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        // 窗体上鼠标按下时
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal)
            {
                // 移动窗体
                this.Capture = false;
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (OperationMode)
            {
                case OperationMode.UploadMt:
                    {
                        FrmExam frm = ParentFrm as FrmExam;
                        if(frm.IsExporting==false)
                        {
                            timer1.Stop();
                            this.Close();
                        }
                    }
                    break;
                case OperationMode.LoadSym:
                    {
                        FrmClass frm = ParentFrm as FrmClass;
                        if (frm.IsLoadingSym == false)
                        {
                            timer1.Stop();
                            this.Close();
                        }
                    }
                    break;
                case OperationMode.ExportWriting:
                    {
                        FrmClass frm = ParentFrm as FrmClass;
                        int all = frm.StudentToExportCount;
                        if (all == 0)
                            all = 1;
                        int fraciton = frm.StudentExportedCount;
                        int percent = (int)(1.0 * fraciton / all*100);
                        
                        labTip.Text =string.Format("正在保存，请稍后 {0}%",percent);
                        labTip.Top = Height / 3;
                        labTip.Left = (Width - labTip.Width - label2.Width) / 2;
                        label2.Left = labTip.Right;
                        label2.Top = labTip.Top - (label2.Height - labTip.Height) / 2;
                        // Debug.WriteLine("{0}   {1}", all, fraciton);
                        if (frm.IsExporting==false)
                        {
                            timer1.Stop();
                            this.Close();
                            frm.ShowExportResult();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
    public enum OperationMode
    {
        UploadMt,
        LoadSym,
        ExportWriting
    }

}
